package com.kunpeng.jvsichuanairspider.bll;

import com.google.common.base.Splitter;
import com.kunpeng.jvsichuanairspider.entity.ShjmptUser;
import com.kunpeng.jvsichuanairspider.utils.DBUtils;
import org.joda.time.DateTime;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/11.
 */
public class Shjmpt {
    //登录；
    public static final String  TOKEN = "Hp0JHpxGKMlGg0JUPln1Q4Fvz0JQBGC22C1979553";
    public static final String TEAMID = "4748";
    //只用第一次获取token;
    DBUtils dbUtils = new DBUtils();
    public String getToken(String userName, String password){
        ShjmptUser shjmptUser = new ShjmptUser();
        shjmptUser.setUserName(userName);
        shjmptUser.setPassword(password);
        ShjmptUser user = dbUtils.getShjmptUser(shjmptUser);
        String token = "";
        DateTime dateTime = new DateTime(user.getUpdateTime());
        if(DateTime.now().isBefore(dateTime.plusMinutes(9))){
            //又token且没有过期；十分钟以内不过期；
            token = user.getToken();
        }else{
            token = loginShjmpt();
            user.setToken(token);
            dbUtils.updateToken(user);
        }
       return token;
    }
    public  String  loginShjmpt(){
        String userName = "nature521";
        String password = "wuge51874894";
        String developer = "zRzvdp%2bNwHfBovX4kXR%2fVQ%3d%3d";
        String url = "http://api.shjmpt.com:9002/pubApi/uLogin?"
                        + "uName=" + userName + "&pWord=" + password
                        + "&Developer=" + developer;
        String response = new CallInterfaceHelper().getMethod(url);
        List<String> responseList = Splitter.on("&").splitToList(response);
        return responseList.get(0) ;
    }
    //获取项目信息；
    public String getTeamInfo(){
        String url = "http://api.shjmpt.com:9002/uGetItems?"
                + "token=" + TOKEN + "&tp=ut";
        String response = new CallInterfaceHelper().getMethod(url);
        return response;
    }

    public String getPhone(int count, String token){
        String url = "http://api.shjmpt.com:9002/pubApi/GetPhone?"
                     + "ItemId=" + TEAMID + "&token=" + token;
        String response = new CallInterfaceHelper().getMethod(url);
        return response;
    }








}
